<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id: Template.php 109 2011-04-11 17:34:22Z ninhgio@gmail.com $
 */
class Modules_Mail_Models_Mysql_Template extends Modules_Mail_Models_Template
{
	protected $_table = 'mail_template';
	
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE template_id = ' . $this->_conn->escape($id) . ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function getByName($name)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE name = "' . $this->_conn->escape($name) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
			
	public function add($template)
	{
		$userId = $this->_conn->insert($template, $this->_conn->_tablePrefix . $this->_table);
		return $userId;	
	}
	
	public function update($template)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET name = "%s", title = "%s", subject = "%s", body = "%s", from_mail = "%s",
						from_name = "%s", reply_to_mail = "%s", reply_to_name = "%s"
					WHERE template_id = "%d"';
		$sql = sprintf($sql, $this->_conn->escape($template['name']),
							$this->_conn->escape($template['title']),
							$this->_conn->escape($template['subject']),
							$this->_conn->escape($template['body']),
							$this->_conn->escape($template['from_mail']), 
							$this->_conn->escape($template['from_name']),
							$this->_conn->escape($template['reply_to_mail']),
							$this->_conn->escape($template['reply_to_name']),
							$this->_conn->escape($template['template_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function delete($id)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE template_id = ' . $this->_conn->escape($id) . ' AND locked = 0';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function getTemplates($userId, $offset = null, $count = null)
	{
		$sql = sprintf("SELECT * FROM " . $this->_conn->_tablePrefix . $this->_table . " 
						WHERE created_user_id = '%s'
						ORDER BY template_id DESC", 
						$this->_conn->escape($userId));
		if (is_int($offset) && is_int($count)) {
			$sql .= sprintf(" LIMIT %s, %s", $offset, $count);
		}
		
		$rs = $this->_conn->query($sql);
		$templates = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$templates[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $templates;
	}
	
	public function count($userId)
	{
		$sql = 'SELECT COUNT(*) as num_templates FROM ' . $this->_conn->_tablePrefix . $this->_table;
		$where = ' WHERE created_user_id = "' . $this->_conn->escape($userId) . '"';
		$sql .= $where;
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_templates'];
	}
}